/*
 * 翻转句子中单词的顺序
 *
 */

#include <stdio.h>
#include <string.h>

int swap(char* one, char* two)
{
    char tmp = *one;
    *one = *two;
    *two = tmp;
}

int reverse_whole(char * word, int start, int end)
{
    while (start < end) {
        swap(word+start, word+end);
        start++; end--;
    }
}

int reverse_word_in_sentence(char * sentence, int number)
{
    reverse_whole(sentence, 0, number-1);
    int i = 0;
    while(i < number-1) {
        int j = i;
        while (' ' != sentence[j] && '\0' != sentence[j]) {
            j++;
        }
        reverse_whole(sentence, i, j-1);
        i = j+1;
    }
    reverse_whole(sentence, i, number-1);
}

int main()
{
    char s[] = "hello world";
    reverse_word_in_sentence(s, strlen(s));
    printf("%s\n", s);
}
